<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Ajax Post 请求</title>
    <style>
      #result {
        width: 200px;
        height: 100px;
        border: solid 1px pink;
      }
    </style>
  </head>
  <body>
    <button>点击发送请求</button>
    <div id="result"></div>
  </body>
  <script type="text/javascript">
    const btn = document.getElementsByTagName('button')[0]
    const result = document.getElementById('result')
    console.log(btn)
    btn.onclick = function(){
        console.log('执行了')
        const xhr = new XMLHttpRequest();
        xhr.open('POST','http://127.0.0.1:8000/server')
        //设置请球球
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
        xhr.setRequestHeader('hhy','sb')
        xhr.send('a=100&b=200&c=300');
        xhr.onreadystatechange = function(){
            //判断
            if(xhr.readyState ===4){
                //判断响应状态码
                if(xhr.status>=200&&xhr.status<300){
                    //处理结果
                    // console.log(xhr.status);
                    // console.log(xhr.statusText);
                    // console.log(xhr.getResponseHeader);//所有响应头
                    // console.log(xhr.response)//响应体
                    result.innerText=xhr.response
                }else{
                    console('出错了',xhr)
                }
            }
        }
    }
  </script>
</html>
